package org.example.product.controller;

import java.util.List;

import org.example.core.utils.R;
import org.example.core.valid.group.AddGroup;
import org.example.product.entity.BrandCategoryEntity;
import org.example.product.service.BrandCategoryService;
import org.example.product.vo.BrandVo;
import org.example.product.vo.RelateCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/pms/backend/brandRelate")
public class BrandCategoryController {
    @Autowired
    private BrandCategoryService brandCategoryService;

    /**
     * 根据品牌id查询关联的分类
     */
    @GetMapping("/list")
    public R<?> list(@RequestParam Long brandId){
        List<RelateCategoryVo>  vos = brandCategoryService.queryCategoryListByBrandId(brandId);
        return R.ok(vos);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R<?> save(@Validated(value = {AddGroup.class}) @RequestBody BrandCategoryEntity brandCategory){
        return brandCategoryService.saveBrandCategory(brandCategory.getBrandId(),brandCategory.getCategoryId())>0? R.ok():R.error();
    }

    /**
     * 根据分类id获取品牌集合
     */
    @GetMapping("/brandList")
    public R<List<BrandVo>> brandListByCategoryId(@RequestParam Long categoryId){
        List<BrandVo> brandVos = brandCategoryService.queryBrandListByCategoryId(categoryId);
        return R.ok(brandVos);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R<?> delete(@PathVariable("id") Long id){
        return brandCategoryService.deleteBrandCategory(id)>0? R.ok():R.error();
    }
}
